package com.gexiaobao.pigeon.ui.fragment.mine.organization.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.hideSoftKeyboard
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.model.bean.AllVisitInfoResponse
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityAllAppointmentInfoBinding
import com.gexiaobao.pigeon.ui.adapter.AllVisitInfoAdapter
import com.gexiaobao.pigeon.viewmodel.OrgDetailViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2024/6/6 16:00
 * @Describe    : 全部预约探视信息
 */
class ActivityAllAppointmentInfo : BaseActivity<OrgDetailViewModel, ActivityAllAppointmentInfoBinding>() {

    private lateinit var loadsir: LoadService<Any>
    private val mAdapter: AllVisitInfoAdapter by lazy { AllVisitInfoAdapter(arrayListOf()) }
    private var mRaceId = 0

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeTitle.tvTitle.text = getString(R.string.All_reservation_information)

        mRaceId = intent.getIntExtra("raceId", 0)

        /** 状态页配置 */
        loadsir = loadServiceInit(mDatabind.includeList.swipeRefreshLayout) {
            /** 点击重试时触发的操作 */
            onRefresh(true)
        }

        onRefresh(true)

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter).let {
            it.initFooter {
                val memberName = mDatabind.includeSearch.editQuery.text.toString().trim()
                mViewModel.getVisitList(false, memberName, mRaceId)
            }
        }

        mDatabind.includeList.swipeRefreshLayout.init {
            onRefresh(true)
        }

        mDatabind.includeSearch.editQuery.addTextChangedListener {
            mDatabind.includeSearch.ivClose.isVisible = it.toString().isNotEmpty()
        }

        mDatabind.includeSearch.editQuery.setOnEditorActionListener { _, i, _ ->
            when (i) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    onRefresh(true)
                    hideSoftKeyboard(this)
                }

                else -> {}
            }
            false
        }
    }

    private fun onRefresh(b: Boolean) {
        val memberName = mDatabind.includeSearch.editQuery.text.toString().trim()
        loadsir.showLoading()
        mViewModel.getVisitList(b, memberName, mRaceId)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        /** 获取全部预定探视会员列表 */
        mViewModel.allVisitInfoResult.observe(this) {
            mDatabind.tvPayState.isGone = !it.payState
            val list: ArrayList<AllVisitInfoResponse.ListBean> = arrayListOf()
            if (!it.listData.isNullOrEmpty()) {
                it.listData.forEachIndexed { _, listBean ->
                    list.add(
                        AllVisitInfoResponse.ListBean(
                            address = listBean.address,
                            memberName = listBean.memberName,
                            pigeonTotal = listBean.pigeonTotal,
                            state = listBean.state,
                            visitType = listBean.visitType,
                            isPayState = it.payState
                        )
                    )
                }
            }
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = it.isRefresh,
                isEmpty = it.isEmpty,
                isFirstEmpty = it.isRefresh && it.isEmpty,
                hasMore = it.hasMore,
                total = it.total,
                listData = list
            )
            loadListData(listDataUiState, mAdapter, loadsir, mDatabind.includeList.swipeRecyclerView, mDatabind.includeList.swipeRefreshLayout)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(
            mDatabind.includeTitle.ivBack,
            mDatabind.includeSearch.ivClose,
            mDatabind.includeSearch.tvSearch,
        ) {
            when (it) {
                mDatabind.includeSearch.tvSearch -> {
                    onRefresh(true)
                }

                mDatabind.includeSearch.ivClose -> {
                    onRefresh(true)
                    mDatabind.includeSearch.editQuery.setText("")
                }

                mDatabind.includeTitle.ivBack -> {
                    finish()
                }
            }
        }
    }
}